package com.immunology.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.immunology.dao.SurveyAnswerDao;
import com.immunology.database.transformers.SurveyAnswerTransformer;
import com.immunology.model.SurveyAnswer;

public class SurveyAnswerService {

	private static SurveyAnswerService surveyAnswerService = null;
	private SurveyAnswerDao surveyAnswerDao = new SurveyAnswerDao(
			new SurveyAnswerTransformer());

	private SurveyAnswerService() {
	}

	public static SurveyAnswerService getInstance() {
		if (surveyAnswerService == null) {
			surveyAnswerService = new SurveyAnswerService();
		}
		return surveyAnswerService;
	}

	public void insert(SurveyAnswer surveyAnswer) {
		surveyAnswerDao.insert(surveyAnswer);
	}

	public Map<Integer, Integer> getComplaintIdValueMapBySurveyId(int surveyId) {
		return surveyAnswerDao.getComplaintIdValueMapBySurveyId(surveyId);
	}

	public List<String> getSumForComplaintTypeValue(
			Map<String, List<Integer>> surveyResultsMap) {
		List<String> resultList = new ArrayList<String>();
		int currentSum = 0;
		int syndromeSum = 0;
		Set<String> set = surveyResultsMap.keySet();
		for (String complaintTypeName : set) {
			List<Integer> list = surveyResultsMap.get(complaintTypeName);
			currentSum += list.get(0);
			syndromeSum += list.get(1);
		}
		resultList.add(String.valueOf(currentSum));
		resultList.add(String.valueOf(syndromeSum));
		resultList.add(String.valueOf(Math.round((currentSum + 0.0f)
				/ syndromeSum * 100)));
		return resultList;
	}
}
